package net.lucamasini.ricette.client.ricette;

import net.lucamasini.ricette.client.ProxyPlace;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class Ricette extends Composite implements RicetteView {
	
	private static IngredientiUiBinder uiBinder = GWT
			.create(IngredientiUiBinder.class);

	interface IngredientiUiBinder extends UiBinder<Widget, Ricette> {
	}

	@UiField CellTable<RicettaProxy> ricetteCellTable;
	@UiField SimplePanel pagerContainer;
	
	@UiField Button crea;
	@UiField Button modifica;
	@UiField Button cancella;

	final SingleSelectionModel<RicettaProxy> selectionModel = new SingleSelectionModel<RicettaProxy>();

	private Presenter presenter;

	public Ricette() {
		initWidget(uiBinder.createAndBindUi(this));

	    SimplePager pager = new SimplePager();
	    pager.setDisplay(ricetteCellTable);
	    pagerContainer.setWidget(pager);
	    
		ricetteCellTable.addColumn(new TextColumn<RicettaProxy>() {

			@Override
			public String getValue(RicettaProxy object) {
				return object.getTitolo();
			}
		}, "TITOLO");

		ricetteCellTable.addColumn(new TextColumn<RicettaProxy>() {

			@Override
			public String getValue(RicettaProxy object) {
				return Integer.toString(object.getTempoPreparazione());
			}
		}, "TEMPO PREP.");

		ricetteCellTable.addColumn(new TextColumn<RicettaProxy>() {

			@Override
			public String getValue(RicettaProxy object) {
				return Integer.toString(object.getDifficolta());
			}
		}, "DIFFICOLTA");

		ricetteCellTable.addColumn(new TextColumn<RicettaProxy>() {

			@Override
			public String getValue(RicettaProxy object) {
				
				return object.getPersone()!=null?Integer.toString(object.getPersone()):"ND";
			}
		}, "PERSONE");

		ricetteCellTable.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				if( selectionModel.getSelectedObject()!=null) {
					selectedButton(true);
				} else {
					selectedButton(false);
				}
			}
		});
	}


	public void unselectRicetteTable() {
		if( selectionModel.getSelectedObject()!=null) {
			selectionModel.setSelected(selectionModel.getSelectedObject(), false);
		}
	}
	
	@UiHandler("crea")
	void onCreaIngrediente(ClickEvent event){
		presenter.goTo(new ProxyPlace(RicettaProxy.class));
	}
	
	@UiHandler("modifica")
	void onModificaIngrediente(ClickEvent event) {
		final RicettaProxy ricetta = selectionModel.getSelectedObject();

		presenter.goTo(new ProxyPlace(ricetta.stableId(), ProxyPlace.Operation.EDIT));
	}
	
	@UiHandler("cancella")
	void onCancellaRicetta(ClickEvent event){
		final RicettaProxy ricetta = selectionModel.getSelectedObject();
		
		presenter.remove(ricetta);
	}
	
	@UiHandler("ricarica")
	void onRicaricaIngrediente(ClickEvent event){
		presenter.goTo(new RicettePlace());
	}

	private void selectedButton(boolean selected) {
		modifica.setEnabled(selected);
		cancella.setEnabled(selected);
	}

	@Override
	public HasData<RicettaProxy> ricette() {
		
		return ricetteCellTable;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
}
